#include "sound.h"

CSound::CSound(System *pSystem, const string &szPath, bool bStream)
    : m_bStream(bStream),
    m_szPath(szPath),
    m_pSystem(pSystem),
    m_fVolume(1.0f),
    m_fMasterVolume(1.0f),
    m_bMute(false)
{
    string szRelativePath("resources/sounds/");
    szRelativePath += m_szPath;

    if (m_bStream)
    {
        m_pSystem->createStream(szRelativePath.c_str(), FMOD_SOFTWARE, 0, 
            &m_pSound);
    }
    else
    {
        m_pSystem->createSound(szRelativePath.c_str(), FMOD_DEFAULT, 0, 
            &m_pSound);
    }
}

CSound::~CSound()
{
    m_pSound->release();
}

void CSound::createNewChannel(Channel **pChannel)
{
    m_pSystem->playSound(FMOD_CHANNEL_FREE, m_pSound, true, pChannel); 
    (*pChannel)->setVolume(getVolume() * m_fMasterVolume);
    (*pChannel)->setMute(m_bMute);
    m_pSystem->update();
}

void CSound::setVolume(float fVolume)
{
    m_fVolume = max(min(fVolume, 1.0f), 0.0f);
}

float CSound::getVolume() const
{
    return m_fVolume;
}

void CSound::setMasterVolume(float fMasterVolume)
{
    m_fMasterVolume = fMasterVolume;
}

void CSound::setMute(bool bMute)
{
    m_bMute = bMute;
}